package controllers

import (
	"matching/engine"
	"matching/middleware/cache"
	"matching/model"
	"matching/process"
	"matching/utils"
	"strconv"
)

type MatchingController struct {
	BaseController
}


func (m *MatchingController) CreateSymbol() {
	symbol:=m.GetString("symbol")
	price:=m.GetString("price")
	coinScale:=m.GetString("coinScale")
	baseCoinScale:=m.GetString("baseCoinScale")
	if !m.paramsCheck(price,symbol,coinScale,baseCoinScale) {
		m.resultError("params not be null")
		return
	}

	if engine.ChanMap[symbol]!=nil {
		//已经启动过
		m.resultError("engine already started")
		return
	}
	cs,_:=strconv.ParseInt(coinScale,10,32)
	bcs,_:=strconv.ParseInt(baseCoinScale,10,32)
	cache.SaveSymbol(symbol)
	info:=model.SymbolInfo{
		Symbol: symbol,
		CoinScale: int32(cs),
		BaseCoinScale: int32(bcs),
	}
	cache.SaveSymbolInfo(info)
	process.CreateEngine(symbol,utils.StringToDecimal(price),int32(cs),int32(bcs))

	m.resultOk()
}

func (m *MatchingController) DeleteSymbol() {
	symbol:=m.GetString("symbol")


	if !m.paramsCheck(symbol) {
		m.resultError("params not be null")
		return
	}

	if engine.ChanMap[symbol]==nil {
		//未启动过
		m.resultError("engine not started")
		return
	}
	process.CloseEngine(symbol)
	m.resultOk()
}